package com.taptech.doufu.ui.view;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.R;
import com.taptech.doufu.listener.ResultTipListener;

/**
 * Created by longsiyang on 15/11/26.
 */
public class ProtocolDialog extends DialogFragment implements View.OnClickListener {


    View rootView;
    Context mContext;

    //UI控件
    ImageView closeWindowBtn;
    TextView sendWorkBtn;
    CheckBox acceptProtocolCheckBox;
    TextView protocolJumpBtn;
    TextView protocolContentTextView;
    // Listener
    ResultTipListener listener;

    // Data
    String contentStr = null;
    String protocolWebUrl = null;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        rootView = inflater.inflate(R.layout.protocol_dialog_layout , container , false);
        mContext = getActivity();
        initData();
        initView();
        return rootView;
    }


    private void initData(){

    }

    private void initView(){
        closeWindowBtn = (ImageView) rootView.findViewById(R.id.close_window_btn);
        closeWindowBtn.setOnClickListener(this);
        sendWorkBtn = (TextView) rootView.findViewById(R.id.send_works_btn);
        sendWorkBtn.setOnClickListener(this);
        acceptProtocolCheckBox = (CheckBox) rootView.findViewById(R.id.accept_protocol_checkbox_id);
        acceptProtocolCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked){
                    sendWorkBtn.setOnClickListener(null);
                    sendWorkBtn.setBackgroundResource(R.drawable.protocol_submit_btn_backgroud_disable);
                }else {
                    sendWorkBtn.setOnClickListener(ProtocolDialog.this);
                    sendWorkBtn.setBackgroundResource(R.drawable.protocol_submit_btn_backgroud_enable);
                }
            }
        });
        protocolJumpBtn = (TextView) rootView.findViewById(R.id.protocol_jump_btn);
        protocolJumpBtn.setOnClickListener(this);
        protocolContentTextView = (TextView) rootView.findViewById(R.id.window_content);
        if (contentStr != null){
            protocolContentTextView.setText(contentStr);
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        int dialogWidth = ScreenUtil2.getScreenWidthPixel((Activity) mContext);
        int dialogHeight = ScreenUtil2.getScreenHeightPixel((Activity) mContext);
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        //window.setLayout(dialogWidth, dialogHeight);
        window.setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
        window.setGravity(Gravity.CENTER);
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_window_btn:
                dismiss();
                break;
            case R.id.send_works_btn:
                if (listener != null){
                    listener.resultBack(1 , 0);
                }
                break;
            case R.id.protocol_jump_btn:
                if (protocolWebUrl != null){
                    BrowseActivity.startActivity(mContext , protocolWebUrl);
                }

                break;
        }
    }

    public void setOnResultListener(ResultTipListener listener){
        this.listener = listener;
    }

    public void setContentStr(String content){
        this.contentStr = content;
    }

    public void setProtocolWebUrl(String url){
        this.protocolWebUrl = url;
    }
}
